package net.oschina.app.improve.detail.fragments;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
//import android.icu.util.Calendar;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.broadcast.AlarmReceiver;
import net.oschina.app.improve.base.activities.BaseBackActivity;
import net.oschina.app.ui.MainActivity;

import org.w3c.dom.Text;

import java.util.Calendar;

public class MySetTimerActivity extends BaseBackActivity{//extends Activity {

    Button setTime;
    AlarmManager aManager;
    Calendar calendar = Calendar.getInstance();
    int rm_year,rm_day,rm_month,rm_hour,rm_minute;
    public static int number_of_clock = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_set_timer);
        setTime = (Button) findViewById(R.id.timeBtn);
        final TextView tex_ = (TextView)findViewById(R.id.confirmBtn);
        // 获取AlarmManager对象

        setTime.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View source)
            {
                //Calendar currentTime = Calendar.getInstance();
                // 创建一个TimePickerDialog实例，并把它显示出来。

                calendar.setTimeInMillis(System.currentTimeMillis());

                new DatePickerDialog(MySetTimerActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        /*calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, month);
                        calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);*/
                        rm_year = year;
                        rm_month = month;
                        rm_day = dayOfMonth;
                        //Toast.makeText(MainActivity.this, "日期设置成功！！", Toast.LENGTH_SHORT).show();
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();

                new TimePickerDialog(MySetTimerActivity.this,new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker arg0, int h, int m) {
                        //更新按钮上的时间
                        //timeBtn.setText(formatTime(h,m));

                        rm_hour = h;
                        rm_minute = m;

                        //Log.d(TAG, "set the time to "+formatTime(h,m));
                    }
                },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show();
            }
        });

        Button cancelAlarmBtn = (Button)findViewById(R.id.cancelAlarmBtn);
        cancelAlarmBtn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View arg0) {



                Intent intent = new Intent(MySetTimerActivity.this,AlarmReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MySetTimerActivity.this, number_of_clock, intent, 0);
                //获取闹钟管理器

                number_of_clock--;

                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                // 取消的只是当前Context的Notification
                mNotificationManager.cancel(0);
                tex_.setText("未设定闹钟");
                Toast.makeText(MySetTimerActivity.this, "提醒1已经取消！", Toast.LENGTH_SHORT).show();
            }
        });

        Button confirmBtn = (Button)findViewById(R.id.confirmBtn);
        confirmBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                tex_.setText(String.valueOf(rm_year)+"年"+String.valueOf(rm_month)+"月"+String.valueOf(rm_day)+"日"+"  "+String.valueOf(rm_hour)+":"+String.valueOf(rm_minute));
                calendar.set(Calendar.YEAR, rm_year);
                calendar.set(Calendar.MONTH, rm_month);
                calendar.set(Calendar.DAY_OF_MONTH,rm_day);
                calendar.set(Calendar.HOUR_OF_DAY, rm_hour);
                calendar.set(Calendar.MINUTE, rm_minute);
                //将秒和毫秒设置为0
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                Context Running_activity = MySetTimerActivity.this;
                EditText UserWord = (EditText) findViewById(R.id.inform);
                Intent intent = new Intent(MySetTimerActivity.this,AlarmRecieverMyself.class);
                intent.putExtra("wordNeedShow",UserWord.getText().toString());
                //sendBroadcast(intent);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(MySetTimerActivity.this, number_of_clock, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                number_of_clock++;
                //获取闹钟管理器
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                //设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                Toast.makeText(MySetTimerActivity.this, "提醒设置成功！！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_detail;
    }
    ;
}